<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/3
 * Time: 11:19
 */
namespace app\index\controller;

use think\Db;
use think\Request;
use think\View;
use think\Controller;

class Zhongchuang extends Controller
{
    public function index()
    {
        $id=getProviderId();
        //省
        $province=Db::name('area')->where('level',0)->select();;
//        var_dump($province);die;
        $this->view->assign('province',$province);
        //市
        $city=Db::name('area')->where('level',1)->select();
        $this->view->assign('city',$city);
        $area=Db::name('area')->where('level',2)->select();
        $this->view->assign('area',$area);
        $data=Db::name('project')->where('status',1)->where('projectType',2)->join('project_station','project.stationId=project_station.stationId')->field('projectId,projectName,project.stationId,location,price,cityId,districtId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit(10)->select();
        //        var_dump($data);
        foreach ($data as $k=> $v){
            $data[$k]['price'] = detailPrice($v['price']);
            //项目图片
            $imgFileId=Db::name('project_gallery')->where('projectId',$v['projectId'])->order('addDateTime','asc')->value('imgFileId');
            $files=Db::name('files')->where('filesId',$imgFileId)->value('thumbFile');
            $data[$k]['files']=$files;
                //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;
        }
        $this->view->assign('data',$data);
        return view('makerspacelist');

    }
    public function search()
    {
        $id=getProviderId();
        if(input('location')!="") {
            $where['districtId'] = input('location');
        }else{
            $where="";
        }
        $price=Db::name('project')->max('price');
        if(input('rent')!="") {
            if(input('rent')==1){
                $wher=[0,1000];
            }elseif(input('rent')==2){
                $wher=[1000.01,5000];
            }elseif(input('rent')==3){
                $wher=[5000.01,10000];
            }elseif(input('rent')==4){
                $wher=[10000.01,$price];
            }
        }else{
            $wher=[0,$price];
        }
        if(input('stamp')!="") {
            if(input('stamp')==7){
                $wh['stationType']=0;
            }elseif(input('stamp')==8){
                $wh['stationType']=1;
            }
        }else{
            $wh="";
        }
        if(input('search')!=""){
            $whe=input('search');
        }else{
            $whe="";
        }
        $data=Db::name('project')->where($where)->where('status',1)->where('projectType',2)->join('project_station','project.stationId=project_station.stationId')->whereLike('projectName','%'.$whe.'%')->whereBetween('price',$wher,'and')->where($wh)->field('projectId,projectName,project.stationId,location,price,cityId,districtId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit(10)->select();
        //        var_dump($data);
        foreach ($data as $k=> $v){
            $data[$k]['price'] = detailPrice($v['price']);
            //项目图片
            $imgFileId=Db::name('project_gallery')->where('projectId',$v['projectId'])->order('addDateTime','asc')->value('imgFileId');
            $files=Db::name('files')->where('filesId',$imgFileId)->value('thumbFile');
            $data[$k]['files']=$files;
            //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;
        }
        return $data;

    }
    public function detail()
    {
        $id=$_GET['projectId'];
        $project=Db::name('project')->where('projectId',$id)->join('project_station','project.stationId=project_station.stationId')->field('projectId,projectName,project.stationId,location,price,cityId,districtId,stationType,projectType,area,memberId,brief,isSelled')->find();
        $project['price'] = detailPrice($project['price']);
        //项目相册
        $imgs=Db::name('project_gallery')->where('projectId',$id)->join('files','project_gallery.imgFileId=files.filesId')->field('thumbFile')->select();
        if($project['memberId']==0){
            $provider=getPlatform();
            $member=$provider['mobile'];
        }else{
            $member=Db::name('member')->where('memberId',$project['memberId'])->value('mobile');
        }
        $project['mobile']=$member;
        //项目类型
        $projectType=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','pjt')->where('optionCode',$project['projectType'])->value('optionName');
        $project['projectType']=$projectType;
        //公司地址
        $city=Db::name('area')->where('areaId',$project['cityId'])->value('areaName');
        $district=Db::name('area')->where('areaId',$project['districtId'])->value('areaName');
        $project['areaName']=$district.$project['location'];
        $project['areaN']=$city.$district.$project['location'];
        //基础设施
        $baseFacility=Db::name('stationfacility')->where('stationId',$project['stationId'])->column('baseFacilityName');
        $facilityName=implode(',',$baseFacility);
        $wher['optionCode']=['in',$facilityName];
        $name=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','bafa')->where($wher)->field('optionName')->select();
        $names=array();
        foreach ($name as $v1){
            $names[]=$v1['optionName'];
        }
        $baseFacilityName=implode(' ',$names);
        $project['baseFacilityName']=$baseFacilityName;
        //基础服务
        $baseService=Db::name('stationservice')->where('stationId',$project['stationId'])->column('baseServiceName');
        $serviceName=implode(',',$baseService);
        $whe['optionCode']=['in',$serviceName];
        $basename=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','basesev')->where($whe)->field('optionName')->select();
        $basenames=array();
        foreach ($basename as $v1){
            $basenames[]=$v1['optionName'];
        }
        $baseServiceName=implode(' ',$basenames);
        $project['baseServiceName']=$baseServiceName;
        //项目相册
        $projectGallery=Db::name('project_gallery')->where('projectId',$id)->column('imgFileId');
        $gallery=implode(',',$projectGallery);
        $wh['filesId']=['in',$gallery];
        $gallerys=Db::name('files')->where($wh)->field('thumbFile')->select();
        $this->view->assign(   'img', $gallerys);
        $this->view->assign('imgs',$imgs);
        $this->view->assign('project', $project);
        return view('makerspaceinfo');
    }
    public function price(){
        $id=getProviderId();
        return $this->view->fetch('price');
    }
    public function getCityDetail2()
    {
        echo 111;
        die;
    }
    public function append()
    {
        if(input('location')!="") {
            if(input('location')=='undefined'){
                $where="";
            }else{
                $where['districtId'] = input('location');
            }
        }else{
            $where="";
        }
        $price=Db::name('project')->max('price');
        if(input('rent')!="") {
            if(input('rent')==1){
                $wher=[0,1000];
            }elseif(input('rent')==2){
                $wher=[1000.01,5000];
            }elseif(input('rent')==3){
                $wher=[5000.01,10000];
            }elseif(input('rent')==4){
                $wher=[10000.01,$price];
            }else{
                $wher=[0,$price];
            }
        }else{
            $wher=[0,$price];
        }
        if(input('stamp')!="") {
            if(input('stamp')==7){
                $wh['stationType']=0;
            }elseif(input('stamp')==8){
                $wh['stationType']=1;
            }else {
                $wh="";
            }
        }else{
            $wh="";
        }
        if(input('search')!=""){
            $whe=input('search');
        }else{
            $whe="";
        }
        $start=input('num')*10;
        $end=$start+10;
        $id=getProviderId();
        $data=Db::name('project')->where($where)->where('status',1)->where('projectType',2)->join('project_station','project.stationId=project_station.stationId')->whereLike('projectName','%'.$whe.'%')->whereBetween('price',$wher,'and')->where($wh)->field('projectId,projectName,project.stationId,location,price,cityId,districtId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit($start,$end)->select();
        foreach ($data as $k=> $v){
            $data[$k]['price'] = detailPrice($v['price']);
            //项目图片
            $imgFileId=Db::name('project_gallery')->where('projectId',$v['projectId'])->order('addDateTime','asc')->value('imgFileId');
            $files=Db::name('files')->where('filesId',$imgFileId)->value('thumbFile');
            $data[$k]['files']=$files;
            //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;
        }
        return $data;
    }
}